/*
 *  PrimaryFlightDisplay.h
 *  framework
 *
 *  Created by gerhard gubler on 17.11.09.
 *  Copyright 2009 md80.ch. All rights reserved.
 *
 */

#include "PrimaryFlightDisplay.h"
#include "OpenGL/gl.h"
#include <math.h>

PrimaryFlightDisplay::PrimaryFlightDisplay(void)
{
	magnitude = 15.0;
}

void PrimaryFlightDisplay::display()
{
	float loopa = 1.6;
	float s_val = 0;
	float c_val = 0;
	
	glLoadIdentity();
	glScalef(magnitude /10, magnitude/10, 0.0);
	glTranslatef(pfd_H_pos, pfd_V_pos ,0.0f);
	glPushMatrix();
	//glTranslatef(250.0f,200.0f +hVal,0.0f);
	glTranslatef(250.0f,200.0f,0.0f);
	glRotatef(pos,0.0,0.0,1.0);
	glTranslatef(-250.0f,-200.0f,0.0f);
	glTranslatef(0.0f,+hVal,0.0f);
	// Sky Rect
	glColor3f(0.0f,0.0f,1.0f);	
	glRectf(10.0,200.0,500.0,500.0f);
	// Ground Rect
	glColor3f(0.6f,0.4f,0.0f);	
	glRectf(10.0,-100.0,500.0,200.0f);
	// Degrees Lines
	glLineWidth(2.0);
	glColor3f(1.0,1.0,1.0);
	glBegin(GL_LINES);
	//Base line
	glVertex2f(100,200);
	glVertex2f(400,200);
	int loop_lines = 0;
	while (loop_lines <200)
	{
	glVertex2f(240,220+loop_lines);
	glVertex2f(260,220+loop_lines);
	glVertex2f(230,235+loop_lines);
	glVertex2f(270,235+loop_lines);
	glVertex2f(240,250+loop_lines);
	glVertex2f(260,250+loop_lines);
	// 10 Degr Line
	glVertex2f(200,260+loop_lines);
	glVertex2f(300,260+loop_lines);
	loop_lines +=60;
	}
	loop_lines = 0;
	while (loop_lines > -200)
	{
	glVertex2f(240,180+loop_lines);
	glVertex2f(260,180+loop_lines);
	glVertex2f(230,165+loop_lines);
	glVertex2f(270,165+loop_lines);
	glVertex2f(240,150+loop_lines);
	glVertex2f(260,150+loop_lines);
	// 10 Degr Line
	glVertex2f(200,140+loop_lines);
	glVertex2f(300,140+loop_lines);
	loop_lines -=60;
	}
	/*
	glVertex2f(240,250);
	glVertex2f(260,250);
	glVertex2f(230,260);
	glVertex2f(270,260);
	glVertex2f(240,270);
	glVertex2f(260,270);
	// 20 Degr Line
	glVertex2f(200,280);
	glVertex2f(300,280);
*/
	glEnd();
	glLineWidth(1.0);
	// Roll bug
	glPopMatrix();
	glPushMatrix();
	glTranslatef(250.0f,200.0f,0.0f);
	glRotatef(pos,0.0,0.0,1.0);
	glTranslatef(-250.0f,-200.0f,0.0f);
	glBegin(GL_TRIANGLES);
	glVertex2f(250.0,350.0);
	glVertex2f(260.0,340.0);
	glVertex2f(240.0,340.0);	
	glEnd();
	glPopMatrix();
	
	//Edging
    // Right UP
	glPushMatrix();
	//display_cir(250.0f, 200.0f, 3.1f, -260.0f, 310.0f, 0.0f, 310.0f);
	
	glColor3f(0.0,0.0,0.0);
	glPopMatrix();
	glPushMatrix();
	glTranslatef(250.0f,200.0f,0.0f);
	glPointSize(3.0); 
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(-260.0,310.0);
	glVertex2f(-0.0,310.0);
	while(loopa < 3.1)
	{
		s_val = sin((float)loopa);
		c_val = cos((float)loopa);
		s_val *=150;
		c_val *=150;
       glVertex2f(c_val,s_val);
	   loopa += 0.1;
	}
	glVertex2f(-260.0,0.0);
	glEnd();
	glPopMatrix();
	
	// Left UP
	glPushMatrix();
	glColor3f(0.0,0.0,0.0);
	glPopMatrix();
	glPushMatrix();
	glTranslatef(250.0f,200.0f,0.0f);
	glPointSize(3.0); 
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(250.0,310.0);
	glVertex2f(250.0,0.0);
	loopa = 0.1;
	while(loopa < 1.7)
	{
		s_val = sin((float)loopa);
		c_val = cos((float)loopa);
		s_val *=150;
		c_val *=150;
       glVertex2f(c_val,s_val);
	   loopa += 0.1;
	}
	 glVertex2f(0.0,310.0);
	 glEnd();
	glPopMatrix();

	// Left DOWN
	glPushMatrix();
	glColor3f(0.0,0.0,0.0);
	glPopMatrix();
	glPushMatrix();
	glTranslatef(250.0f,200.0f,0.0f);
	glPointSize(3.0); 
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(260.0,-150.0);
	//glVertex2f(-0.0,-310.0);
	loopa = 3.1;
	while(loopa > 1.4)
	{
		s_val = sin((float)loopa);
		c_val = cos((float)loopa);
		s_val *=150;
		c_val *=150;
       glVertex2f(s_val,c_val);
	   loopa -= 0.1;
	}
	glEnd();
	glPopMatrix();

	// Right DOWN
	glPushMatrix();
	glColor3f(0.0,0.0,0.0);
	glPopMatrix();
	glPushMatrix();
	glTranslatef(250.0f,200.0f,0.0f);
	glPointSize(3.0); 
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(-200.0,-150.0);
	//glVertex2f(-0.0,-310.0);
	loopa = 4.8;
	while(loopa > 3.0)
	{
		s_val = sin((float)loopa);
		c_val = cos((float)loopa);
		s_val *=150;
		c_val *=150;
       glVertex2f(s_val,c_val);
	   loopa -= 0.1;
	}
	glEnd();
	//glColor3f(0.0,1.0,0.0);
	 glRectf(-350.0,300.0,-120.0,-200.0);
	 glRectf(120.0,300.0,350.0,-200.0);
	 //glColor3f(0.0,1.0,0.0);
	 glRectf(-350.0,-149.0,350.0,-300.0);
	glPopMatrix();
		 
	// Gray parts (Fillers for the left and right wing and the middle quad.
	glLineWidth(1.0);
	glColor3f(0.5,0.5,0.5);
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(350,203);
	glVertex2f(270,203);
	glVertex2f(270,197);
	glVertex2f(350,197);
	glEnd();
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(270,170);
	glVertex2f(280,170);
	glVertex2f(280,197);
	glVertex2f(270,197);
	glEnd();
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(245,205);
	glVertex2f(255,205);
	glVertex2f(255,195);
	glVertex2f(245,195);
	glEnd();
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(150,203);
	glVertex2f(230,203);
	glVertex2f(230,197);
	glVertex2f(150,197);
	glEnd();	
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(220,170);
	glVertex2f(220,197);
	glVertex2f(230,197);
	glVertex2f(230,170);
	glEnd();

	glColor3f(1.0,1.0,1.0);
	//glRectf(150,200,350,204);
	glLineWidth(3.0);
	glBegin(GL_LINE_LOOP);
	//Left side wing
	glVertex2f(150,203);
	glVertex2f(230,203);
	glVertex2f(230,170);
	glVertex2f(220,170);
	glVertex2f(220,197);
	glVertex2f(150,197);
	glEnd();
	glBegin(GL_LINE_LOOP);
    //Middle Quad
	glVertex2f(245,205);
	glVertex2f(255,205);
	glVertex2f(255,195);
	glVertex2f(245,195);
	glEnd();
	glBegin(GL_LINE_LOOP);
	//Right side wing
	glVertex2f(350,203);
	glVertex2f(270,203);
	glVertex2f(270,170);
	glVertex2f(280,170);
	glVertex2f(280,197);
	glVertex2f(350,197);
	glEnd();
	glLineWidth(1.0);
}

// Calculates and draws a cirular line.
void PrimaryFlightDisplay::display_cir(float x, float y, float arc_l, float bxpos, float bypos, float expos, float eypos)
{
	float loopa = 0.1;
	
	glColor3f(0.0,0.0,0.0);
	glPushMatrix();
	glPopMatrix();
	glTranslatef(x,y,0.0f);
	glPointSize(3.0);
	glBegin(GL_TRIANGLE_FAN);
	glVertex2f(bxpos,bypos);
	glVertex2f(expos,eypos);
	while(loopa < arc_l)
	{
		float s_val = sin((float)loopa);
		float c_val = cos((float)loopa);
		s_val *=pos;
		c_val *=pos;
       glVertex2f(c_val,s_val);
	   loopa += 0.1;
	}
	glVertex2f(-260.0,0.0);
	glEnd();
	glPushMatrix();
}
